<?php
/**
 * Created by PhpStorm.
 * User: 86151
 * Date: 2023/9/19
 * Time: 11:07
 */

namespace app\admin\model;

use think\Db;
use think\Model;
class Acxq extends Model
{
    /**
     * 订单查询接口
     */
    public static  function info(){
        $methodName='info';
        $url='https://bdshdandu.zhisanzhao.com/koubei/channelProviderNotify/order/info';
        $array =[
            'out_order_sn'=>'202307111989654',
        ];
        $res = self::send($methodName,$url,$array);
        return $res;
    }
    /**
     * 退款接口
     */
    public static  function  refund($out_order_sn){
         $methodName='refund';
         $url='https://bdshdandu.zhisanzhao.com/koubei/channelProviderNotify/order/refund';
         $array =[
             'out_order_sn'=>$out_order_sn,
         ];
         $res = self::send($methodName,$url,$array);
         return $res;
     }
    /**
     * 创建订单接口
     */
    public static  function createOrder($out_order_sn){
        $methodName='create';
        $url='https://bdshdandu.zhisanzhao.com/koubei/channelProviderNotify/order/create';
        $array =[
            'out_order_sn'=>$out_order_sn,
        ];
        $res = self::send($methodName,$url,$array);
        return $res;
    }

    /**
     * 校验下单接口
     */
    public static  function inspect($item_id,$sku_id,$phone,$out_order_sn,$activity_price_cent){
        $methodName='inspect';
        $url='https://bdshdandu.zhisanzhao.com/koubei/channelProviderNotify/order/inspect';
        $array =[
            'item_id'=>$item_id,
            'sku_id'=>(string)$sku_id,
            'phone'=>$phone,
            'out_order_sn'=>$out_order_sn,
            'activity_price_cent'=>(int)$activity_price_cent,
        ];
        $res = self::send($methodName,$url,$array);
        return $res;
    }
    /**
     *  商品详情
     */
    public static function getItemInfo($item_id){
        $methodName='getItemInfo';
        $url='https://bdshdandu.zhisanzhao.com/koubei/channelProviderNotify/getItemInfo';
        $array =[
            'item_id'=>$item_id,
        ];
        $res = self::send($methodName,$url,$array);
        return $res;
    }
    /**
     * 获取商品列表
     */
    public static function goodslist($page,$limit,$city_code){
        $methodName='getProductList';
        $url='https://bdshdandu.zhisanzhao.com/koubei/channelProviderNotify/getProductList';
        $array =[
            'page'=>$page,
            'limit'=>$limit,
            'city_code'=>$city_code,
        ];

        $res = self::send($methodName,$url,$array);
        return $res;
    }

    /**
     * 获取城市信息
     */
    public static function city(){
        $array =[];
        $methodName='getCity';
        $url='https://bdshdandu.zhisanzhao.com/koubei/channelProviderNotify/getCity';
        $res = self::send($methodName,$url,$array);
        return $res;
    }

    /**
     * 参数传输
     */
    static private function send($methodName,$url,$array){
        $appKey= 'd88d0749f4ec459a89f200396eb5cbd2';
        $arr['timeStamp'] = time();
        $arr['secret'] ='971abb8c92b6462c9954339f020ef602';
        $sign = self::createSign($arr,$array);
        $paramArrs[] = 'appKey:'.$appKey;
        $paramArrs[] = 'timeStamp:'.$arr['timeStamp'];
        $paramArrs[] = 'methodName:'.$methodName;
        $paramArrs[] = 'sign:'.$sign;
        $paramArrs[] =  'Content-Type:application/json';
        if(count($array)>0){
            $array = json_encode($array);
        }else{
            $array = '';
        }
        $res = self::curlPost($url,$array,$paramArrs);
        $res = json_decode($res,true);
        return $res;
    }

    static private function curlPost($url,$post_data,$paramArrs){
        $header  = $paramArrs;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch , CURLOPT_URL , $url);
        curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch , CURLOPT_POST, 1);
        curl_setopt($ch , CURLOPT_POSTFIELDS, $post_data);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
    static private function createSign ($paramArr,$array) {
        $sign = '';
        if(count($array)>0){
            $paramArr = array_merge($paramArr,$array);
        }
        ksort($paramArr);
        foreach ($paramArr as $key => $val) {
            if ($key != '' && $val != '') {
                $sign .=$key.$val;
            }
        }
        $sign = strtoupper(md5(urlencode($sign)));
        return $sign;
    }
}